<?php
namespace App\Repos;

use App\Models\Billboard;
use App\Models\BillboardPic;
use App\Services\RedisService;
use Carbon\Carbon;

class BillboardRepo 
{
    public static function get($where=[])
    {   
        $today = date('Y-m-d');
    	$list = Billboard::where($where)
                ->whereDate('created_at',$today)
                ->orderBy('created_at','desc')
                ->get();
        foreach ($list as $k => $v) {
            $date = $v->work_time;
            $carbon = Carbon::parse ($date); // 格式化一个时间日期字符串为 
            $v->mins = (new Carbon)->diffInMinutes($carbon, true);
            $hours = (new Carbon)->diffInHours($carbon, true);
            if($hours<2) {
                $v->is_new = 1;
                $v->point_style = 'g1';
                $v->txt  = '直播';
                $v->btn_style = 'danger';
            }else{
                $v->is_new = 0;
                $v->point_style = 'g0';
                $v->txt = '已查';
                $v->btn_style = 'primary';
            }
            if($v->mins>59){
                $v->pass_time = $hours.'小时前';
            } else {
                $v->pass_time = $v->mins.'分钟前';
            }
            $v->hours = $hours;
        }

        return $list;
    }

    public static function pages($where=[],$fields=['*'],$num=10)
    {
    	$query = Billboard::query();
        if(array_get($where,'code')){
            $code = array_pull($where,'code');
            $query->where('code','like','%'.$code.'%');
        }
        $list = $query->where($where)
    						->select($fields)
                            ->orderBy('id','desc')
    						->paginate($num);
    	return $list;					
    }

    public static function find($where=[],$fields=['*'])
    {     
     
        return Billboard::where($where)->first();    
        
    }

    public static function save($data)
    {
        $pics = array_pull($data,'pics');
        $pics = explode(',', rtrim($pics,','));
        if($pics) {
            $data['thumb'] = $pics[0];
        }
        $flag   = Billboard::create($data);
      

        
        foreach ($pics as $k => $v) {


            $bp['billboard_id'] = $flag->id;
            $bp['pic'] = $v;

            BillboardPic::create($bp);
        }
        
       
        return $flag;
    }

    public static function update($id,$data)
    {
       $pics = array_pull($data,'pics');
       $pics = explode(',', rtrim($pics,','));
       $flag   =  Billboard::where(['id'=>$id])->update($data);
       if($flag) {
        foreach ($pics as $k => $v) {


            $bp['billboard_id'] = $id;
            $bp['pic'] = $v;

            BillboardPic::create($bp);
        }

            
        }
      
       return $flag;
    }

    public static function delete($id)
    {
        $flag = Billboard::destroy($id);
       
        return $flag;
    }

    public static function rank()
    {
        return ['AAA级'=>'AAA级','AAAA级'=>'AAAA级','AAAAA级'=>'AAAAA级'];
    }

    public static function pics($billboard_id)
    {
        return BillboardPic::where('billboard_id',$billboard_id)->get();
    }
}
